library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## Warning: package 'readr' was built under R version 4.0.5
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
data <- as.data.frame(iris)
head(data)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
p1 <- ggplot(data, aes(Petal.Length, Petal.Width)) +
geom_point(aes(color = Species), position = 'jitter') +
scale_color_manual(values = c('#7B435B', '#9FA0C3', '#BCF8EC')) +
theme_minimal() +
ylab('Petal Width') +
xlab('Petal Length') +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())
p1
ggplotlyggplotly(p1)
plot_lysetosa <- data %>% filter(Species == 'setosa')
versicolor <- data %>% filter(Species == 'versicolor')
virginica <- data %>% filter(Species == 'virginica')
p2 <- plot_ly(type = 'scatter', mode = 'markers')
p2 <- p2 %>%
add_trace(
x = setosa$Petal.Length,
y = setosa$Petal.Width,
marker = list(
color = '#7B435B',
size = 10),
name = 'Setosa'
)
p2
p2 <- p2 %>%
add_trace(
x = versicolor$Petal.Length,
y = versicolor$Petal.Width,
marker = list(
color = '#9FA0C3',
size = 10),
name = 'Versicolor'
)
p2
p2 <- p2 %>%
add_trace(
x = virginica$Petal.Length,
y = virginica$Petal.Width,
marker = list(
color = '#BCF8EC',
size = 10),
name = 'Virginica'
)
p2
p2 <- plot_ly(type = 'scatter', mode = 'markers')
p2 <- p2 %>%
add_trace(
x = setosa$Petal.Length,
y = setosa$Petal.Width,
marker = list(
color = '#7B435B',
size = 10),
name = 'Setosa',
hovertemplate = 'Petal Width: %{y} <br>Petal Length: %{x}'
)
p2 <- p2 %>%
add_trace(
x = versicolor$Petal.Length,
y = versicolor$Petal.Width,
marker = list(
color = '#9FA0C3',
size = 10),
name = 'Versicolor',
hovertemplate = 'Petal Width: %{y} <br>Petal Length: %{x}'
)
p2 <- p2 %>%
add_trace(
x = virginica$Petal.Length,
y = virginica$Petal.Width,
marker = list(
color = '#BCF8EC',
size = 10),
name = 'Virginica',
hovertemplate = 'Petal Width: %{y} <br>Petal Length: %{x}'
)
p2
p2 <- plot_ly(type = 'scatter', mode = 'markers')
p2 <- p2 %>%
add_trace(
x = setosa$Petal.Length,
y = setosa$Petal.Width,
marker = list(
color = '#7B435B',
size = 10),
name = 'Setosa',
hovertemplate = 'Petal Width: %{y} <br>Petal Length: %{x}'
)
p2 <- p2 %>%
add_trace(
x = versicolor$Petal.Length,
y = versicolor$Petal.Width,
marker = list(
color = '#9FA0C3',
size = 10),
name = 'Versicolor',
hovertemplate = 'Petal Width: %{y} <br>Petal Length: %{x}'
)
p2 <- p2 %>%
add_trace(
x = virginica$Petal.Length,
y = virginica$Petal.Width,
marker = list(
color = '#BCF8EC',
size = 10),
name = 'Virginica',
hovertemplate = 'Petal Width: %{y} <br>Petal Length: %{x}'
)
p2 <- p2 %>%
layout(title = 'Scatterplot of Petal Length by Petal Width',
xaxis = list(title = 'Petal Length', showgrid = F),
yaxis = list(title = 'Petal Width', showgrid = F, zeroline = F))
p2
Homework:
# Referenced plotly-1.html from class to complete this
p3 <- plot_ly(type = 'scatter3d', mode = 'markers')
p3 <- p3 %>%
add_trace(
x = setosa$Petal.Length,
y = setosa$Petal.Width,
z = setosa$Sepal.Length,
marker = list(
color = '#E389B9',
size = 10,
line = list(
color = 'lightgray',
width = .5
)
),
name = 'Setosa',
hovertemplate = 'Petal Width: %{y} <br>Petal Length: %{x} <br>Sepal Length: %{z}'
)
p3 <- p3 %>%
add_trace(
x = versicolor$Petal.Length,
y = versicolor$Petal.Width,
z = versicolor$Sepal.Length,
marker = list(
color = '#288BA8',
size = 10,
line = list(
color = 'lightgray',
width = .5
)),
name = 'Versicolor',
hovertemplate = 'Petal Width: %{y} <br>Petal Length: %{x} <br>Sepal Length: %{z}'
)
p3 <- p3 %>%
add_trace(
x = virginica$Petal.Length,
y = virginica$Petal.Width,
z = virginica$Sepal.Length,
marker = list(
color = '#746AB0',
size = 10,
line = list(
color = 'lightgray',
width = .5
)),
name = 'Virginica',
hovertemplate = 'Petal Width: %{y} <br>Petal Length: %{x} <br>Sepal Length: %{z}'
)
p3 <- p3 %>%
layout(title = 'Scatterplot of Petal Length by Petal Width by Sepal Length',
scene = list(xaxis = list(title = "Petal Length"),
yaxis = list(title = "Petal Length"),
zaxis = list(title = "Sepal Length")))
p3
# Sites referenced to complete this code (though none of it is copied or a "chunk" of code):
# https://plotly.com/r/box-plots/#grouped-box-plots
# https://www.geeksforgeeks.org/how-to-create-grouped-box-plot-in-plotly/
# https://www.rdocumentation.org/packages/plotly/versions/4.10.0/topics/add_trace
p4 <- plot_ly(type = 'box')
x1 = c(replicate(50, 'Petal Length'), replicate(50, 'Petal Width'), replicate(50, 'Sepal Length'))
p4 <- p4 %>% add_trace(
type='box',
x = x1,
y = c(versicolor$Petal.Length, versicolor$Petal.Width, versicolor$Sepal.Length),
color = '#482C3D',
name = 'Versicolor'
)
p4 <- p4 %>%
add_trace(
type='box',
x = x1,
y = c(setosa$Petal.Length, setosa$Petal.Width, setosa$Sepal.Length),
color = '#537D8D',
name = 'Setosa'
)
p4 <- p4 %>%
add_trace(
type='box',
x = x1,
y = c(virginica$Petal.Length, virginica$Petal.Width, virginica$Sepal.Length),
color = '#E5D352',
name = 'Virginica'
)
p4 <- p4 %>%
layout(title = 'Grouped Boxplot of Petal Length, Petal Width, and Sepal Length by Species', boxmode='group', xaxis = list(showgrid = F, zeroline = T),
yaxis = list(title = 'Centimeters', showgrid = F, zeroline = T))
p4
## Warning: Can't display both discrete & non-discrete data on same axis
## Warning: 'layout' objects don't have these attributes: 'boxmode'
## Valid attributes include:
## '_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'